﻿Public Class FMGuruMengajar

    'member variables 
    Private _csemester As CSemester
    Private _cguru As CGuru
    Private _cmengajar As CGuruMengajar

    'forms
    Private WithEvents _flSemester As FLSemester

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown, MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint, PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If ctl.Visible Then
                p.Color = ctl.BackColor
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                    g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
                ElseIf TypeOf ctl Is Panel Then
                    PaintTextBox(ctl, g, p)
                ElseIf TypeOf ctl Is GroupBox Then
                    PaintTextBox(ctl, g, p)
                End If
            End If

        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(sender As System.Object, e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        Else
            btnMinimize.Visible = False
            btnMaximize.Visible = False
            btnClose.Visible = False
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region


    Private Sub TBSemester_Enter(sender As Object, e As System.EventArgs) Handles TBSemester.Enter, TBSemester.Click
        'tampilkan form list semester
        'form cover
        Dim fcov As New FCover
        fcov.Show()

        _flSemester = New FLSemester
        _flSemester.ShowDialog()

        fcov.Close()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        'set semester terpilih
        TBSemester.Text = _flSemester.SelectedNamaSemester
        tbSemesterValue.Text = _flSemester.SelectedIDSemester
    End Sub

    Private Sub FMGuruMengajar_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'set datagrid
        DGMasterGuru.AutoGenerateColumns = False
        DGMasterKelas.AutoGenerateColumns = False
        DGMasterMataPelajaran.AutoGenerateColumns = False
        'set semester aktif
        _csemester = New CSemester
        TBSemester.Text = _csemester.getAktifSemester.nama
        tbSemesterValue.Text = _csemester.getAktifSemester.id
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'menampilkan data guru
        _cguru = New CGuru
        DGMasterGuru.DataSource = _cguru.getAll
    End Sub

    Private Sub DGMasterKelas_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGMasterKelas.CellContentClick
        'set dgmaster MP enable
        If DGMasterKelas.CurrentRow.Cells("cpilihkelas").EditedFormattedValue Then
            gbMP.Enabled = True
        Else
            gbMP.Enabled = False
        End If
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMasterGuru.RowPostPaint, DGMasterKelas.RowPostPaint, DGMasterMataPelajaran.RowPostPaint
        'menampilkan nomor urut pada datagridview
        sender.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
    End Sub

    Private Sub DGMasterGuru_SelectionChanged(sender As Object, e As System.EventArgs) Handles DGMasterGuru.SelectionChanged
        'tampilkan data kelas
        Try
            _cmengajar = New CGuruMengajar
            DGMasterKelas.DataSource = _cmengajar.getKelasLeftJoin(DGMasterGuru.SelectedRows(0).Cells("cid").Value, tbSemesterValue.Text.Trim)
        Catch ex As Exception
            If ex.Message.Contains("out of range") Then
                'handle kesalahan out of range yang gak penting
            Else
                MsgBox(ex.Message)
            End If

        End Try

    End Sub

    Private Sub DGMasterKelas_SelectionChanged(sender As Object, e As System.EventArgs) Handles DGMasterKelas.SelectionChanged
        'tampilkan daftar mata pelajaran yang di ajar

        Try
            'DGMasterMataPelajaran.DataSource = _cmengajar.getMP(DGMasterGuru.SelectedRows(0).Cells("CID").Value, tbSemesterValue.Text.Trim, DGMasterKelas.SelectedRows(0).Cells("CIDkelas").Value)

            DGMasterMataPelajaran.DataSource = _cmengajar.getMataPelajaranLeftJoin(DGMasterGuru.SelectedRows(0).Cells("CID").Value, DGMasterKelas.SelectedRows(0).Cells("CIDkelas").Value, tbSemesterValue.Text.Trim)

            'set enable data mata pelajaran
            If DGMasterKelas.CurrentRow.Cells("cpilihkelas").EditedFormattedValue Then
                gbMP.Enabled = True
            Else
                gbMP.Enabled = False
            End If
        Catch ex As Exception
            If ex.Message.Contains("out of range") Then
                'handle kesalahan out of range yang gak penting
            Else
                MsgBox(ex.Message)
            End If
        End Try

    End Sub

   
    Private Sub SimpanToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        btnSimpan.PerformClick()
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        btnKeluar.PerformClick()
    End Sub

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpan.Click
        'simpan perubahan data
        'buat data table (id_guru,id_mp,id_kls,id_smstr)
        Dim dTabMengajar As New DataTable
        With dTabMengajar
            .Columns.Add("id_guru")
            .Columns.Add("id_mp")
            .Columns.Add("id_kls")
            .Columns.Add("id_smstr")
        End With

        For Each aRow As DataGridViewRow In DGMasterMataPelajaran.Rows
            If DGMasterKelas.CurrentRow.Cells("cpilihkelas").EditedFormattedValue Then
                If aRow.Cells("CPilih").EditedFormattedValue Then
                    Dim dRow As DataRow = dTabMengajar.NewRow
                    With dRow
                        dRow.Item("id_guru") = DGMasterGuru.CurrentRow.Cells("cid").Value
                        dRow.Item("id_mp") = aRow.Cells("cidmatapelajaran").Value
                        dRow.Item("id_kls") = DGMasterKelas.CurrentRow.Cells("cidkelas").Value
                        dRow.Item("id_smstr") = tbSemesterValue.Text.Trim
                    End With
                    dTabMengajar.Rows.Add(dRow)
                End If
            End If
        Next

        Try
            _cmengajar = New CGuruMengajar
            _cmengajar.simpan(DGMasterGuru.CurrentRow.Cells("cid").Value, DGMasterKelas.CurrentRow.Cells("cidkelas").Value, tbSemesterValue.Text.Trim, dTabMengajar)

            MessageBox.Show("Simpan data berhasil", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class
